package util.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import ui.model.MessageBean;

public class DBManager {
    private DBHelper helper;
    private SQLiteDatabase db;

    public DBManager(Context context) {
        helper = new DBHelper(context);
        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
        db = helper.getWritableDatabase();
    }

    /**
     * 批量add persons
     *
     * @param persons
     */

    public void add(List<MessageBean> persons) {
        db.beginTransaction();    //开始事务
        try {
            for (MessageBean person : persons) {
                db.execSQL("INSERT INTO messageTable VALUES(?, ?, ?, ?,?, ?, ?)", new Object[]{person.getMessageType(), person.getContentInfo(),
                        person.getFromInfo(), person.getToInfo(), person.getTimeInfo(), person.getSignInfo(), person.getType()});
            }
            db.setTransactionSuccessful();    //设置事务成功完成
        } finally {
            db.endTransaction();    //结束事务
        }
    }

    /**
     * 单个add persons
     *
     * @param person
     */
    public void add(MessageBean person) {
        db.beginTransaction();    //开始事务
        try {
            db.execSQL("INSERT INTO messageTable VALUES(?, ?, ?, ?, ?, ?, ?, ? )", new Object[]{person.getMessageType(), person.getContentInfo(),
                    person.getFromInfo(), person.getToInfo(), person.getTimeInfo(), person.getSignInfo(), person.getType(), person.getUserId()});
            db.setTransactionSuccessful();    //设置事务成功完成
        } finally {
            db.endTransaction();    //结束事务
        }
    }


    /**
     * update person's age
     *
     * @param person
     */
    public void updateAge(MessageBean person) {
        ContentValues cv = new ContentValues();
        cv.put("age", person.getContentInfo());
        db.update("messageTable", cv, "name = ?", new String[]{person.getType()});
    }

    /**
     * delete old person
     *
     * @param person
     */
    public void deleteOldPerson(MessageBean person) {
        db.delete("messageTable", "timeInfo >= ?", new String[]{String.valueOf(person.getTimeInfo())});
    }

    /**
     * delete old person
     *
     * @param
     */
    public void deleteALL(String[] timeInfo) {
        db.delete("messageTable", "timeInfo >= ?", timeInfo);
    }

    /**
     * query all persons, return list
     *
     * @return List<Person>
     */

    public List<MessageBean> query(String userId) {
        ArrayList<MessageBean> persons = new ArrayList<MessageBean>();
        Cursor c = queryTheCursor(userId);
        while (c.moveToNext()) {
            MessageBean person = new MessageBean();
            person.setMessageType(c.getInt(c.getColumnIndex("MessageType")));
            person.setContentInfo(c.getString(c.getColumnIndex("contentInfo")));
            person.setFromInfo(c.getString(c.getColumnIndex("fromInfo")));
            person.setToInfo(c.getString(c.getColumnIndex("toInfo")));
            person.setTimeInfo(c.getLong(c.getColumnIndex("timeInfo")));
            person.setSignInfo(c.getString(c.getColumnIndex("signInfo")));
            person.setType(c.getString(c.getColumnIndex("type")));
            person.setUserId(c.getString(c.getColumnIndex("userId")));
            persons.add(person);
        }
        c.close();
        return persons;
    }

    /**
     * query all persons, return cursor
     *
     * @return Cursor
     */
    public Cursor queryTheCursor(String userId) {
        Cursor c = db.rawQuery("SELECT * FROM messageTable WHERE userId = ? ", new String[]{userId});
        return c;
    }

    /**
     * close database
     */
    public void closeDB() {
        db.close();
    }
}
